/**
 * 用户模块
 */
define(['jquery', 'jquery.validate', 'jquery.form','jquery.validate.addmethod','jquery.ajax.ext','jquery.enplaceholder'], function($, validate, form){

	return {
		api : function(){
			$(function(){
				$('input[placeholder]').placeholder({isUseSpan:true});
				$("form").submit(function(){
					return false;
				});
				/*body高度*/
				(function(){
					/*var bodyHeight = $('body').height();
					setBodyHeight(bodyHeight);
					$(window).resize(function(){
						setBodyHeight(bodyHeight);
					});
					function setBodyHeight(bodyHeight){
						if(bodyHeight < $(window).height()){
							$('body').height($(window).height());
						}else{
							$('body').height(bodyHeight);
						}
					}*/
				})();
				
				/*选中第一个输入框*/

				if($("#valid_phone_form").length<=0)
				{
					$('form:visible').find('input.form_control').eq(0).focus();
				}
				//.blur();
				var smsInterval;
				function getTimeUnit(sec)
				{
					//sec = Math.ceil(sec/1000);
					//console.log(sec);
					var val=sec,unit="秒";
					if(sec>3600)
					{
						val = Math.round(sec/3600);
						unit ="小时";
					}
					else if(sec>60)
					{
						val = Math.round(sec/60);
						unit ="分钟";
					}
					return [val,unit];
				}
				function smsBtnInterval(time)
				{
					//time = time || new Date().getTime();
					var sec = time?time-Math.round(new Date().getTime()/1000):60;
					//console.log(sec);
					if(sec<=0)
					{
						return resetSmsBtn();
					}
					var btn = $(".send-sms");
					var v = getTimeUnit(sec);
					btn.attr("disabled",true).html("<span class='second'>"+v[0]+"</span>"+v[1]+"后重发");
					sec--;
					smsInterval = setInterval(function(){
						if(sec<=0)
						{
							resetSmsBtn();
							btn.attr("disabled",false).html("重发");
							return;
						}
						var v = getTimeUnit(sec);
						btn.attr("disabled",true).html("<span class='second'>"+v[0]+"</span>"+v[1]+"后重发");
						sec--;
					},1000);
				}

				function resetSmsBtn()
				{
					$(".send-sms").attr("disabled",true).text("发送");
					clearInterval(smsInterval);
				}

				function show_error(msg)
				{
					$("#user_info_form_msg").removeClass("success").show().find("span").text(msg);
				}

				function show_success(msg)
				{
					$("#user_info_form_msg").addClass("success").show().find("span").text(msg);
				}

				//邀请码开关
				var inviteFlag = $('.register-invite-body').length > 0 ? true : false;

				var regPhoneConf = {
					/*onfocusout : function(element) {
					 $(element).valid();
					 },*/
					showErrors:function(error,arr){
						this.defaultShowErrors(arguments);
						if(error.phone)
						{
							$(".send-sms").attr("disabled",true);
						}
						$.each(arr,function(k,v){
							//console.log(v.element);
							var el = $(v.element);
							el.parent().find('.success').removeClass('right_icon');
						});
					},
					rules: {
						phone:{
							required:true,
							//require_from_group:[1,'.group-phone'],
							mobile_phone:true,
							custom_remote:
							{
								url:"/register/check_phone",
								successHandler:function(ret){
									if(ret.extra_params && ret.extra_params.live_time)
									{
										//继续上次的倒计时
										smsBtnInterval(ret.extra_params.live_time);
									}
									else
									{
										resetSmsBtn();
										$(".send-sms").attr("disabled",false);
									}
								},
								errorHandler:function(ret){
									if(ret.extra_params && ret.extra_params.live_time)
									{
										//继续上次的倒计时
										smsBtnInterval(ret.extra_params.live_time);
									}
									else
									{
										resetSmsBtn();
									}
								},
								beforeSend:function(){
									$(".send-sms").attr("disabled",true);
								}
							}
						},
						sms_vcode:{
							required:true,
							depend:"#phone_1",
							number:true,
							rangelength:[6,6],
							custom_remote:{
								url:'/register/check_sms_verify_code',
								data: {
									sms_vcode: function(){
										return $("[name=sms_vcode]").val();
									},
									phone:function() {
										return $("#phone_1").val();
									}
								}
							}
						},
						n_email: {
							maxlength: 60,
							check_email: true,
							custom_remote:{
								url:'/register/check_email',
								data: {
									email: function(){
										return $("[name=n_email]").val();
									}
								}
							}
						},
						password: {
							required: true,
							rangelength: [8, 16],
							check_password: true
						}/*,
						 confirm_password: {
						 required: true,
						 check_password: true,
						 rangelength: [8, 16],
						 equalTo: "#password"
						 }*/,
						"protocol[]": {
							required: true
						},
						code: {
							required: true,
							remote: {
								url: '/register/check_code',     //后台处理程序
								type: 'post',               //数据发送方式
								data: {                     //要传递的数据
									'code': function(){
										return $('#code').val();
									}
								}
							}
						}
					},
					messages: {
						phone:{
							required:"手机号不能为空",
							mobile_phone:"手机号格式错误"
						},
						sms_vcode:{
							required:"短信验证码不能为空",
							maxlength:$.validator.format("请输入{0}位数字验证码"),
							number:"请输入纯数字验证码",
							rangelength:$.validator.format("请输入{0}位数字验证码"),
							depend:"请先填写正确的手机号码"
						},
						n_email: {
							maxlength: $.validator.format('邮箱不允许超过{0}个字符'),
							check_email: '邮箱地址格式错误'
						},
						password: {
							required: '密码不允许为空',
							rangelength: $.validator.format('由{0}-{1}位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字')
						}/*,
						 confirm_password: {
						 required: '确认密码不允许为空',
						 rangelength: $.validator.format('由{0}-{1}位字母、数字、符号组成,区分大小写且不允许有空格,不能是纯字母或数字'),
						 equalTo: '两次输入的密码不一致'
						 }*/,
						"protocol[]": {
							required: '请先阅读并同意《用户协议》'
						},
						code: {
							required: '请输入验证码',
							remote: '验证码错误'
						}

					},
					errorPlacement:function(error,element){
						$("#reg_form").find('.notice').remove();
						element.parent().find('.success').removeClass('right_icon');
						element.parent().find("label.checked").remove();
						//console.log(arguments);
						/*console.log("heihei");
						 if(element.attr('type') == 'password'){
						 if(element.val() == ''){
						 error.addClass('pwd_empty');
						 } else {
						 error.removeClass('pwd_empty');
						 }
						 }*/

						if(element.attr('data-id') == 'protocol'){
							error.appendTo(element.parent().parent());
						} else {
							error.appendTo(element.parent());
						}
					},
					success: function(label,input) {

						//console.log(label);
						//label.html('').attr('class', 'checked');
						var input = $(input);
						input.removeClass("error");
						if(input.is("[name=phone]") && $(".send-sms").find(".second").length<=0)
						{
							$(".send-sms").attr("disabled",false);
						}
						if(input.val()!='')
						{
							label.parent().find('.success').addClass('right_icon');
						}

					}
				};
				//邀请码回调检测
				if (inviteFlag) {
					regPhoneConf.rules.invitation_code = {
						required: true,
						custom_remote: {
							url: '/register/check_invitation_code',     //后台处理程序
							type: 'post',               //数据发送方式
							data: {                     //要传递的数据
								'invitation_code': function () {
									return $('#invitation').val();
								}
							}
						}
					};

					regPhoneConf.messages.invitation_code = {
						required: '请输入邀请码',
						remote: '邀请码错误'
					};
				}

				var regByPhoneFormValid = $("#reg_by_phone").validate(regPhoneConf);

				/*$("#phone_1").keydown(function(){
					$(this).attr("old-value",$(this).val());
				});*/
				$("#phone_1").keyup(function(e){
					if(/^1[0-9]{10}$/.test($(this).val()))
					{
						$(this).valid();
					}
				});
				//var LOCK_LIST={};
				//发送短信
				$("#reg_by_phone .send-sms").click(function(e)
				{
					var me = $(this);
					var phoneDom = $("#phone_1");
					var mobile = phoneDom.val();
					if(!phoneDom.valid())
					{
						me.attr("disabled",true);
						return false;
					}
					me.attr("disabled",true).text("正在发送...");
					//var lockList = phoneDom.data("lockList") ||{};
					$("#user_info_form_msg").hide();
					$.post("/register/send_sms",{phone:mobile},function(ret){
						if(ret.success===true)
						{
							//var msg = "<span class='second'>60</span>秒后重发";
							smsBtnInterval();
							//lockList.mobile={msg:msg,time:new Date().getTime()};
						}
						else
						{
							if(ret.extra_params && ret.extra_params.live_time)
							{
								smsBtnInterval(ret.extra_params.live_time);
							}
							else
							{
								resetSmsBtn();
								me.attr("disabled",false).html("重发");
							}
							show_error(ret.message);
						}
						//phoneDom.data("lockList",lockList);
					},'json');
					return false;
				});

				var regEmailConf = {
					/*onfocusout : function(element) {
					 $(element).valid();
					 },*/
					showErrors:function(error,arr){
						this.defaultShowErrors(arguments);
						/*if(error.phone)
						 {
						 $(".send-sms").attr("disabled",true);
						 }*/
						$.each(arr,function(k,v){
							//console.log(v.element);
							var el = $(v.element);
							el.parent().find('.success').removeClass('right_icon');
						});
					},
					rules: {
						email:{
							required:true,
							maxlength:60,
							//require_from_group:[1,'.group-phone'],
							check_email:true,
							custom_remote:
							{
								url:"/register/check_email",
								parseMessage:function(ret)
								{
									if(ret.error_code == 5010)
									{
										return '该账户未激活可<a href="http://' + ret.host + '" target="_blank">立即激活</a>，如没收到点击<a href="javascript:void(0);" data-email="' + ret.email + '" id="ajax_resend_register_email">重新发送</a>';
									}
									else
									{
										return ret.message;
									}
								}
							}
						},
						password: {
							required: true,
							rangelength: [8, 16],
							check_password: true
						},
						confirm_password: {
							required: true,
							check_password: true,
							rangelength: [8, 16],
							equalTo: "#password_2"
						},
						"protocol[]": {
							required: true
						},
						n_phone: {
							mobile_phone: true,
							custom_remote:{
								url:'/register/check_phone',
								data: {
									phone: function()
									{
										return $("[name=n_phone]").val();
									},
									ignore:true
								}
							}
						},
						code: {
							required: true,
							custom_remote: {
								url: '/register/check_code',     //后台处理程序
								type: 'post',               //数据发送方式
								data: {                     //要传递的数据
									'code': function(){
										return $('#code').val();
									}
								}
							}
						},
					},
					messages: {
						n_phone:{
							mobile_phone:"手机号格式错误"
						},
						email: {
							required:"邮箱不能为空",
							maxlength: $.validator.format('邮箱不允许超过{0}个字符'),
							check_email: '邮箱地址格式错误'
						},
						password: {
							required: '密码不允许为空',
							rangelength: $.validator.format('由{0}-{1}位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字')
						},
						confirm_password: {
							required: '确认密码不允许为空',
							rangelength: $.validator.format('由{0}-{1}位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字'),
							equalTo: '两次输入的密码不一致'
						},
						"protocol[]": {
							required: '请先阅读并同意《用户协议》'
						},
						custom_remote: {
							required: '请输入验证码',
							remote: '验证码错误'
						}

					},
					errorPlacement:function(error,element){
						$("#reg_form").find('.notice').remove();
						element.parent().find('.success').removeClass('right_icon');
						element.parent().find("label.checked").remove();
						if(element.attr('data-id') == 'protocol'){
							error.appendTo(element.parent().parent());
						} else {
							error.appendTo(element.parent());
						}
					},
					success: function(label,input) {

						//console.log(label);
						//label.html('').attr('class', 'checked');
						var input = $(input);
						input.removeClass("error");
						if(input.val()!='')
						{
							label.parent().find('.success').addClass('right_icon');
						}

					}
				};
				if (inviteFlag) {
					regEmailConf.rules.invitation_code = {
						required: true,
						custom_remote: {
							url: '/register/check_invitation_code',     //后台处理程序
							type: 'post',               //数据发送方式
							data: {                     //要传递的数据
								'invitation_code': function () {
									return $('#invitation2').val();
								}
							}
						}
					};

					regEmailConf.messages.invitation_code = {
						required: '请输入邀请码',
						remote: '邀请码错误'
					};
				}

				//邮箱注册
				var regByEmailFormValid = $("#reg_by_email").validate(regEmailConf);

				//提交注册
				$("#register_submit").click(function(){
					var btn = $(this);
					if($("#reg_by_phone").is(":visible") && $("#reg_by_phone").valid())
					{
						var data = 	$("#reg_by_phone").serializeArray();
						btn.text("正在注册...").attr("disabled",true);
						$.post('/register/register_by_phone',data,function(ret){
							if(ret.success===true)
							{
								btn.text("注册成功")/*.attr("disabled",true)*/;
								show_success("注册成功，马上进入系统...");
								setTimeout(function(){
									location.href = ret.redirect;
								},1000);
							}
							else
							{
								btn.text("注册").attr("disabled",false);
								show_error(ret.message);
							}
						},'json');
					}
					else if($("#reg_by_email").is(":visible") && $("#reg_by_email").valid())
					{
						var data = 	$("#reg_by_email").serializeArray();
						btn.text("正在注册...").attr("disabled",true);
						$.post('/register/register_by_email',data,function(ret){
							if(ret.success===true)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.text("注册").attr("disabled",false);
								show_error(ret.message);
							}
						},'json');
					}
				});
				var resendInterval;
				function resendIntervalFunc(a,text1,text2,sec)
				{
					var msg = text2 || "%u秒后重新发送";
					var text = text1 || "重新发送";
					sec= sec || 60;
					msg = msg.replace("%u",sec);
					sec--;
					resendInterval =setInterval(function(){
						if(sec<=0)
						{
							clearInterval(resendInterval);
							a.text(text);
							return;
						}
						msg.replace("%u",getTimeUnit(sec));
						a.text(msg);
						sec--;
					},1000);
				}
				$("body").delegate("#ajax_resend_register_email","click",function(e){
					e.stopPropagation();
					var me= $(this);
					var email = me.attr("data-email");
					var pr = me.parent();
					pr.html("正在发送...");
					$.post("/register/resend_register_email",{email:email},function(ret)
					{
						if(ret.success)
						{
							location.href = ret.redirect;
						}
						else
						{
							var suffix = ret.error_code!=6004?' <a href="javascript:void(0);" data-email="' + email + '" id="ajax_resend_register_email">重新发送</a>':"";
							pr.html(ret.message+suffix);
						}

					},'JSON');
				});


				/*$(".protocol :checkbox").click(function(e){
					();
				});*/
				$(".protocol").click(function(){
					var me = $(this);
					var checkbox = me.find(":checkbox");
					var i = me.find("i");
					if(checkbox.prop("checked"))
					 {
					    checkbox.prop("checked",false);
					 	i.removeClass("active");
					 }
					 else
					 {
					    checkbox.prop("checked",true);
					 	i.addClass("active");
					 }
					 checkbox.valid();
				});
				/*$(".protocol_checkbox").click(function(){
					var me = $(this);
					var checkbox = me.next(":checkbox");
					checkbox.trigger("click");
					//console.log(checkbox.prop("checked"));
					*//*if(checkbox.attr("checked"))
					{
						//checkbox.attr("checked",false).trigger("change");
						me.removeClass("active");
					}
					else
					{
						//checkbox.attr("checked",true).trigger("change");
						me.addClass("active");
						//me.nextAll("label.error").hide();
					}
					return false;*//*
					//checkbox.click();
					//var valid = checkbox.valid();
				});
				$(".protocol_checkbox").next(":checkbox").change(function(){
					var me=$(this);
					console.log(me.prop("checked"));
				});*/

				/*$('#password').keyup(function(){
					if($(this).val() == ''){
						$('#password-error').addClass('pwd_empty');
					} else {
						$('#password-error').removeClass('pwd_empty');
					}
				});*/
				/*$("#reg_form").find('.register_btn').on('click', function(){
					if($('#email-error').text() != ''){
						return false;
					}
					if($("#reg_form").valid()){
						if($('#reg_form').find("input[name='email']").data('check') != 1){
							return false;
						}
						return true;
					}
					return false;
				});
				
				
				$('#reg_form').find("input[name='email']").on({
					'keyup' : check_reg,
					'blur' : check_reg
				});*/
				
				
				
				/*function check_reg(){
					var $that = $('#reg_form').find("input[name='email']");
					var email = $.trim($that.val());
					if(email == ''){
						return;
					}
					//console.log($('#email-error').text());
					if($('#email-error').text() != ''){
						return;
					}
					$that.data('check', 0);
					$.ajax({
						type : 'post',
						url  : '/register/check_email',
						data : {email: email},
						dataType : 'json',
						success : function(result){
							if(result.ret != 1){
								$that.parent().find('.success').removeClass('right_icon');
								$that.parent().find("label").remove();
								$that.addClass('error');
							}
							if(result.ret == 2){
								var msg = '<label id="email-error" class="error" for="email">该邮箱已被注册</label>';
								$that.parent().append(msg);
							} else if(result.ret == 3){
								var msg = '<label id="email-error" class="error" for="email">该账户未激活可<a href="http://' + result.data + '" target="_blank">立即激活</a>，如没收到点击<a href="javascript:void(0);" id="verify_reset_send">重新发送</a></label>';
								$that.parent().append(msg);
							}
							$that.data('check', 1);
						}
					});
				}*/
				$("#login_form").validate({
					//errorClass:"no-error-cls",
					showErrors:function(error,arr){
						var len = arr.length;
						$.each(arr,function(k,v){
							if(k==0)
							{
								$("#form_error").html(v.message);
							}
						});
						if(len==0){
							$("#form_error").html("");
						}
						/*if(len==0)
						{
							$("#form_error").text("");

							//判断邮箱是否已经注册且并未验证
							var email = $("#email").val();
							var eReg = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/;
							if (eReg.test(email)) {
								console.log("is email");
								$.ajax({
									type: "POST",
									url: "/register/check_email",
									data: {"email":email},
									success: function(res) {
										var ret = JSON.parse(res);
										if(ret.error_code == 5010) {
											var msg = '该账户未激活可<a href="http://' + ret.host + '" target="_blank">立即激活</a>，如没收到点击<a href="javascript:void(0);" data-email="' + ret.email + '" id="ajax_resend_register_email">重新发送</a>';
											$("#form_error").text("").append(msg);
										}
									}
								});
							}
						}*/
					},/*
					success:function(ret){
						console.log(arguments);
						$("#form_error").text("1111");
					},*/
					rules:{
						account:
						{
							required:true,
							regex:/^1[0-9]{10}$|([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}/,
							custom_remote:{
								url:"/login/check_email",
								data: {
									email: function(){
										return $("[name=account]").val();
									}
								},
								parseMessage:function(ret)
								{
									if(ret.error_code == 5010)
									{
										return '该账户未激活可<a href="http://' + ret.host + '" target="_blank">立即激活</a>，如没收到点击<a href="javascript:void(0);" data-email="' + ret.email + '" id="ajax_resend_register_email">重新发送</a>';
									}
									else
									{
										return ret.message;
									}
								},
								beforeSendHandler:function(value){
									var eReg = /^([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/;
									if (!eReg.test(value)) {
										return false;
									}

								}
							}
						},
						password:
						{
							required:true
						},
						code:{
							required:true
						}
					},
					messages:{
						account:
						{
							required:"请输入手机号码或邮箱账号",
							regex:"账号格式有误"
						},
						password:
						{
							required:"请输入密码"
						},
						code:
						{
							required:"请填写验证码"
						}
					}
				});

				$('#login_btn').on('click', function(){
					var form = $("#login_form");
					var btn = $(this);
					if(form.valid())
					{
						btn.attr("disabled",true).text("登录中...");
						$.post("/login/do_login",form.serializeArray(),function(ret)
						{

							if(ret.success==true)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("登录");
								if(ret.show_vcode)
								{
									var html = '<label class="field" for="code">验证码</label><input class="form_control" type="text" name="code" id="code" value="" tabindex="3"><img id="js_img_code" class="img_code" src="/login/code?t='+new Date().getTime()+'" alt="" />';
									$('.item_code').html(html);
									//$('#email').addClass('error').focus();
								}
								$('#form_error').html(ret.message);
							}
						},'json');
					}
					return false;
					/*if(!login_check_email()){
						$('#email').focus();
						return false;
					}
					if(!login_check_password()){
						$('#password').focus();
						return false;
					}
					ajax_form_sub(setErrorTime());
					return false;*/

				});
				$('#login_form').find('.password').keyup(function(e){
					if(e.keyCode==13)
					{
						$('#login_btn').trigger("click");
					}
				});
				/*$('#login_form').find('.email').blur(function(){
					login_check_email();
				});
				$('#login_form').find('.password').blur(function(){
					login_check_password();
				});*/
				function login_check_email(){
					if($('#email').val() == ''){
						var error_info = '<label id="email-error" class="error" for="email">邮箱不允许为空</label>';
						$('#form_error').html(error_info);
						$('#email').addClass('error');
						return false;
					} else {
						//$('#form_error').html('');
						$('#email').removeClass('error');
						return true;
					}
				}
				function login_check_password(){
					if($('#password').val() == ''){
						var error_info = '<label id="error-password" class="error" for="password">密码不允许为空</label>';
						$('#form_error').html(error_info);
						$('#password').addClass('error');
						return false;
					} else {
					//	$('#form_error').html('');
						$('#password').removeClass('error');
						return true;
					}
				}
				
				/*登录ajax提交*/
				function ajax_form_sub(time){
					$.ajax({
						type: 'post',
						url : '/login/run_login',
						dataType: 'json',
						data:  $("#login_form").serialize(),
						success: function(result){
							clearTimeout(time);
							$('#form_error').html('');
							$('#login_form').find('input.error').removeClass('error');
							if(result.ret == 1){
								window.location.href = "/files";
							} else if(result.ret == 2){
								$('#login_form')[0].reset();
								var html = '<label class="field" for="code">验证码</label><input class="form_control" type="text" name="code" id="code" value=""><img id="js_img_code" class="img_code" src="/login/code" alt="" />';
								$('.item_code').html(html);
								var error_info = '<label id="email-error" class="error" for="email">'+result.error+'</label>';
								$('#email').addClass('error').focus();
								$('#form_error').html(error_info);
							} else if(result.ret == 3){
								var error_info = '<label id="error_info" class="error" for="code">验证码不正确</label>';
								$('#code').addClass('error').focus();
								$('#form_error').html(error_info);
							} else if(result.ret == 4){
								var error_info = '<label id="email-error" class="error" for="email">'+result.error+'</label>';
								$('#email').addClass('error').focus();
								$('#form_error').html(error_info);
							}
							else if(result.ret == 5){
								var error_info = '<label id="email-error" class="error" for="email">该账户未激活可<a href="http://' + result.data + '" target="_blank">立即激活</a>，如没收到点击<a href="javascript:void(0);" id="verify_reset_send">重新发送</a></label>';
								$('#email').addClass('error').focus();
								$('#form_error').html(error_info);
							}
						}
					});
				}
				
				$('body').on('click', '#verify_reset_send', function(){
					var email = $.trim($('#email').val());
					email = encodeURIComponent(email);
					window.location.href="/register/verify_reset_send/" + email;
					/*$.post('/register/reset_send', {email:email}, function(result){
						email = encodeURIComponent(email);
						window.location.href="/register/verify_reset_send/" + email;
					});*/
				});
				
				
				
				
				/*checkbox模拟*/
				function checkbox_analog(obj){
					if($(obj).size() == 0){
						return false;
					}
					$('.js_checkbox_icon').on('click', function(){
						if($(obj).size() == 0){
							return false;
						}
						var protocolObj = $(obj).get(0);
						if(protocolObj.checked == false){
							$('.checkbox_icon').addClass('active');
							protocolObj.checked = true;
						} else {
							$('.checkbox_icon').removeClass('active');
							protocolObj.checked = false;
						}
					});
				}
				function check_controller(obj){
					if($(obj).size() == 0){
						return false;
					}
					var protocolObj = $(obj).get(0);
					if(protocolObj.checked == true){
						$('.checkbox_icon').addClass('active');
					} else {
						$('.checkbox_icon').removeClass('active');
					}
				}
				checkbox_analog('#protocol');
				checkbox_analog('#auto_login');
				check_controller('#protocol');
				check_controller('#auto_login');
				
				
				/*强制密码只能输入16位*/
				$('#password, #confirm_password').on('keyup', function(){
					var val = $(this).val();
					if(val.length >= 17){
						$(this).val(val.substr(0, 16));
					}
				}); 
				
				/*验证码更新*/
				$('body').delegate('.img_code', 'click', function(){
					var me=$(this);
					me.attr('src',me.attr("src")+"?"+new Date().getTime());
				});
				
				/*重新发邮件 一分钟内只能发一次*/
				$('#send_email').click(function(){
					if($(this).is(".disabled"))
					{
						return false;
					}
					var me = $(this);
					var form = $('#resend_form');
					var data = form.serializeArray();
					$('.msg-box').remove();
					me.addClass("disabled").parents('.to_btn').append('<label class="msg-box">正在发送...</label>');
					$.post(form.attr("action"),data,function(result){
						me.removeClass("disabled");
						var error_msg = '<label class="msg-box error-box"><i class="user_icons error-icon"></i> ';
						var succ_msg = '<label class="msg-box">';
						var msg = '';
						if(result.success==1)
						{
							succ_msg += '重发邮件成功';
							error_msg = "";

						}
						else{
							error_msg += result.message;
						}
						msg += error_msg == '' ? succ_msg:error_msg;
						msg += '</label>';
						if($('.msg-box').size() == 1){
							$('.msg-box').remove();
						}
						$('#send_email').parents('.to_btn').append(msg);
					},'json');
				});
				
				/*重新发邮件 一分钟内只能发一次*/
				$('#active_send_email').click(function(){
					var form = $('#resend_form');
					var data = form.serializeArray();
					if($('.msg-box').size() == 1){
						$('.msg-box').remove();
					}
					$('#active_send_email').parents('.to_btn').append('<label class="msg-box">正在发送...</label>');
					$.post('/register/resend_register_email',data,function(result){
						var error_msg = '<label class="msg-box error-box"><i class="user_icons error-icon"></i> ';
						var succ_msg = '<label class="msg-box">';
						var msg = '';
						if(result.success==true)
						{
							succ_msg += '重发激活邮件成功';
							error_msg = "";
						}
						else{
							error_msg += result.message;
						}
						msg += error_msg == '' ? succ_msg:error_msg;
						msg += '</label>';
						if($('.msg-box').size() == 1){
							$('.msg-box').remove();
						}
						$('#active_send_email').parents('.to_btn').append(msg);
					},'json');
				});
				
				/*找回密码验证邮箱*/
				$("#find_pwd_form").validate({
					rules: {
						account: {
							required: true,
							regex:/1[0-9]{10}|([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}/
						},
						code:{
							required:true
						},
						vcode:
						{
							required:true
						},
						password:
						{
							required:true,
							check_password:true
						}
					},
					messages: {
						account: {
							required: '请输入手机或邮箱',
							regex: '请输入正确格式的手机或邮箱'
							//email: '邮箱地址错误'
						},
						code:{
							required:"短信验证码不能为空"
						},
						vcode:
						{
							required:"验证码不能为空"
						},
						password:{
							required:"密码不能为空"
						}
					},
					errorPlacement:function(error,element){
						element.parent().find("label.checked").remove();
						error.appendTo(element.parent());
					},
					success: function(label) {
						label.html('').attr('class', 'checked');
					}
				});
				$("#find_pwd_form").find('.step1').on('click', function(){
					var form = $("#find_pwd_form");
					var btn = $(this);
					if(form.valid()){
						btn.attr("disabled",true).text("操作中...");
						$.post("/find_pwd/do_find_pwd",form.serializeArray(),function(ret){

							if(ret.success)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("下一步");
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});
				$('#resend_sms').on('click', function(){
					var btn = $(this);
					if(!btn.is(".loading")){
						btn.addClass("loading").html('发送中...<i class="user_icons"></i>');
						$.post("/find_pwd/resend_sms",{},function(ret){
							btn.removeClass("loading").html('重新发送<i class="user_icons"></i>');
							if(ret.success)
							{
								$("#user_info_form_msg").addClass("success").show().find("span").text("发送成功");
							}
							else
							{
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});
				$("#find_pwd_form").find('.step2').on('click', function(){
					var form = $("#find_pwd_form");
					var btn = $(this);
					if(form.valid()){
						btn.attr("disabled",true).text("操作中...");
						$.post("/find_pwd/check_sms_code",form.serializeArray(),function(ret){

							if(ret.success)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("下一步");
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});
				$("#reset_pwd_form").find('.step3').on('click', function(){
					var form = $("#reset_pwd_form");
					var btn = $(this);
					if(form.valid()){
						btn.attr("disabled",true).text("操作中...");
						$.post("/find_pwd/do_reset_pwd_by_sms",form.serializeArray(),function(ret){

							if(ret.success)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("提交");
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});


				
				/*重置密码验证*/
				$("#reset_pwd_form").validate({
					rules: {
						password: {
							required: true,
							check_password: true,
							rangelength: [8, 16]
						},
						confirm_password: {
							required: true,
							equalTo: "#password",
							rangelength: [8, 16]
						}
					},
					messages: {
						password: {
							required: '密码不允许为空',
							rangelength: $.validator.format('由{0}-{1}位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字')
						},
						confirm_password: {
							required: '确认密码不允许为空',
							equalTo: '两次输入的密码不一致',
							rangelength: $.validator.format('由{0}-{1}位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字')
						}
					},
					errorPlacement:function(error,element){
						$("#reset_pwd_form").find('.notice').remove();
						element.parent().find('.success').removeClass('right_icon');
						element.parent().find("label.checked").remove();
						if(element.attr('name') == 'password'){
							if(element.val() == ''){
								error.addClass('pwd_empty');
							} else {
								error.removeClass('pwd_empty');
							}
							
						}
						if(element.attr('id') == 'protocol'){
							 error.appendTo(element.parent().parent());
						} else {
							 error.appendTo(element.parent());
						}
					},
					success: function(label) {
						label.html('').attr('class', 'checked');
						//label.parent().find('.success').addClass('right_icon');
					}
				});
				$("#reset_pwd_form").find('.submit_reset_pwd').on('click', function(){
					var form = $("#reset_pwd_form");
					if(form.valid()){
						var btn = $(this);
						btn.attr("disabled",true).text("操作中...");
						$.post("/find_pwd/do_reset_pwd",form.serializeArray(),function(ret){
							if(ret.success && ret.redirect)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("确认");
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});
				
				/*用户资料验证*/
				/*if($.validator){
					//检查手机号码

				};*/
				$("#user_info_form").validate({
					rules: {
						nickname:{
							required:true,
							maxlength:30
						},
						company_name:{
							//required:true,
							maxlength:30
						},
						n_mobile: {
							//required:true,
							mobile_phone: true,
							custom_remote:{
								url:'/register/check_phone',
								data: {
									phone: function()
									{
										return $("#mobile").val();
									},
									ignore:true
								}
							}
						},
						n_email:{
							//required:true,
							maxlength: 60,
							check_email: true,
							custom_remote:{
								url:'/register/check_email',
								data: {
									email: function(){
										return $("[name=n_email]").val();
									}
								}
							}
						}
						
					},
					messages: {
						nickname:{
							required: '请填写昵称',
							maxlength: $.validator.format('昵称长度不能大于{0}个字符')
						},
						company_name:{
							required: '请填写公司名称',
							maxlength: $.validator.format('公司名称长度不能大于{0}个字符')
						},
						n_mobile:{
							required:"手机号码不能为空",
							mobile_phone:"手机格式错误"
						},
						n_email:{
							required:"邮箱不能为空",
							maxlength: $.validator.format('邮箱不允许超过{0}个字符'),
							check_email: '邮箱地址格式错误'
						}
					},
					errorPlacement:function(error,element){
						//element.parent().find("label.checked").remove();
						var id = element.attr("id");
						$("[for='"+id+"']").append(error);
					}/*,
					success: function(label) {
						//label.html('').attr('class', 'checked');
					}*/
				});

				/*var newPhoneFormValid = $("#new_phone_form").validate({
					/!*onfocusout : function(element) {
					 $(element).valid();
					 },*!/
					showErrors:function(error,arr){
						this.defaultShowErrors(arguments);
						if(error.phone)
						{
							$(".send-sms").attr("disabled",true);
						}
						$.each(arr,function(k,v){
							//console.log(v.element);
							var el = $(v.element);
							el.parent().find('.success').removeClass('right_icon');
						});
					},
					rules: {
						mobile:{
							required:true,
							//require_from_group:[1,'.group-phone'],
							mobile_phone:true,
							custom_remote:
							{
								url:"/user/check_phone",
								successHandler:function(ret){
									if(ret.extra_params && ret.extra_params.live_time)
									{
										$(".send-sms").attr("disabled",true);
										//继续上次的倒计时
										smsBtnInterval(ret.extra_params.live_time);
									}
									else
									{
										resetSmsBtn();
										$(".send-sms").attr("disabled",false);
									}
								},
								errorHandler:function(ret){
									if(ret.extra_params && ret.extra_params.live_time)
									{
										//继续上次的倒计时
										smsBtnInterval(ret.extra_params.live_time);
									}
									else
									{
										resetSmsBtn();
									}
								},
								beforeSendHandler:function(){
									$(".send-sms").attr("disabled",true);
								}
							}
						},
						code:{
							required:true,
							depend:"#mobile",
							number:true/!*,
							rangelength:[6,6]*!/
						}
					},
					messages: {
						mobile:{
							required:"手机号不能为空",
							mobile_phone:"手机号格式错误"
						},
						code:{
							required:"短信验证码不能为空",
							maxlength:$.validator.format("{0}位数字验证码"),
							rangelength:$.validator.format("{0}位数字验证码"),
							//number:"请输入数字验证码",
							depend:"请先填写正确的手机号码"
						}
					},
					errorPlacement:function(error,element){
						$("#reg_form").find('.notice').remove();
						element.parent().find('.success').removeClass('right_icon');
						element.parent().find("label.checked").remove();
						//console.log(arguments);
						/!*console.log("heihei");
						 if(element.attr('type') == 'password'){
						 if(element.val() == ''){
						 error.addClass('pwd_empty');
						 } else {
						 error.removeClass('pwd_empty');
						 }
						 }*!/

						if(element.attr('data-id') == 'protocol'){
							error.appendTo(element.parent().parent());
						} else {
							error.appendTo(element.parent());
						}
					},
					success: function(label,input) {

						//console.log(label);
						//label.html('').attr('class', 'checked');
						var input = $(input);
						input.removeClass("error");
						if(input.is("[name=phone]") && $(".send-sms").find(".second").length<=0)
						{
							$(".send-sms").attr("disabled",false);
						}
						/!*if(input.val()!='')
						{
							label.parent().find('.success').addClass('right_icon');
						}*!/

					}
				});
				$("#new_phone_form .send-sms").click(function(e)
				{
					var me = $(this);
					var phoneDom = $("#mobile");
					var mobile = phoneDom.val();
					if(!phoneDom.valid())
					{
						me.attr("disabled",true);
						return false;
					}
					me.attr("disabled",true).text("正在发送...");
					//var lockList = phoneDom.data("lockList") ||{};
					$("#user_info_form_msg").hide();
					$.post("/user/send_sms",{mobile:mobile},function(ret){
						if(ret.success===true)
						{
							//var msg = "<span class='second'>60</span>秒后重发";
							smsBtnInterval();
							//lockList.mobile={msg:msg,time:new Date().getTime()};
						}
						else
						{
							if(ret.extra_params && ret.extra_params.live_time)
							{
								smsBtnInterval(ret.extra_params.live_time);
							}
							else
							{
								resetSmsBtn();
								me.attr("disabled",false).html("重发");
							}
							$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
						}
						//phoneDom.data("lockList",lockList);
					},'json');
					return false;
				});

				$("#new_phone_form").find('.save_new_phone').on('click', function(){
					var form = $("#new_phone_form");
					if(form.valid()){
						var btn = $(this);
						btn.attr("disabled",true).text("操作中...");
						$.post("/user/save_new_phone",form.serializeArray(),function(ret){
							if(ret.success && ret.redirect)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("提交");
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});*/
				var urlCheckPhone = $("#url_check_phone").val();

				var validPhoneFormValid = $("#valid_phone_form").validate({
					/*onfocusout : function(element) {
					 $(element).valid();
					 },*/
					showErrors:function(error,arr){
						this.defaultShowErrors(arguments);
						if(error.phone)
						{
							$(".send-sms").attr("disabled",true);
						}
						$.each(arr,function(k,v){
							//console.log(v.element);
							var el = $(v.element);
							el.parent().find('.success').removeClass('right_icon');
						});
					},
					rules: {
						mobile:{
							required:true,
							//require_from_group:[1,'.group-phone'],
							mobile_phone:true,
							custom_remote:
							{
								url:urlCheckPhone,
								successHandler:function(ret){
									if(ret.extra_params && ret.extra_params.live_time)
									{
										$(".send-sms").attr("disabled",true);
										//继续上次的倒计时
										smsBtnInterval(ret.extra_params.live_time);
									}
									else
									{
										resetSmsBtn();
										$(".send-sms").attr("disabled",false);
									}
								},
								errorHandler:function(ret){
									if(ret.extra_params && ret.extra_params.live_time)
									{
										//继续上次的倒计时
										smsBtnInterval(ret.extra_params.live_time);
									}
									else
									{
										resetSmsBtn();
									}
								},
								beforeSend:function(){
									$(".send-sms").attr("disabled",true);
								}
							}
						},
						code:{
							required:true,
							depend:"#mobile",
							number:true/*,
							rangelength:[6,6]*/
						}
					},
					messages: {
						mobile:{
							required:"手机号不能为空",
							mobile_phone:"手机号格式错误"
						},
						code:{
							required:"短信验证码不能为空",
							//number:$.validator.format("{0}位数字验证码"),
							maxlength:$.validator.format("{0}位数字验证码"),
							rangelength:$.validator.format("{0}位数字验证码"),
							depend:"请先填写正确的手机号码"
						}
					},
					errorPlacement:function(error,element){
						$("#reg_form").find('.notice').remove();
						element.parent().find('.success').removeClass('right_icon');
						element.parent().find("label.checked").remove();
						//console.log(arguments);
						/*console.log("heihei");
						 if(element.attr('type') == 'password'){
						 if(element.val() == ''){
						 error.addClass('pwd_empty');
						 } else {
						 error.removeClass('pwd_empty');
						 }
						 }*/

						if(element.attr('data-id') == 'protocol'){
							error.appendTo(element.parent().parent());
						} else {
							error.appendTo(element.parent());
						}
					},
					success: function(label,input) {

						//console.log(label);
						//label.html('').attr('class', 'checked');
						var input = $(input);
						input.removeClass("error");
						if(input.is("[name=phone]") && $(".send-sms").find(".second").length<=0)
						{
							$(".send-sms").attr("disabled",false);
						}
						/*if(input.val()!='')
						 {
						 label.parent().find('.success').addClass('right_icon');
						 }*/

					}
				});
				$("#valid_phone_form .send-sms").click(function(e)
				{
					var me = $(this);
					var phoneDom = $("#mobile");
					var mobile = phoneDom.val();
					if(!phoneDom.valid())
					{
						me.attr("disabled",true);
						return false;
					}
					me.attr("disabled",true).text("正在发送...");
					//var lockList = phoneDom.data("lockList") ||{};
					$("#user_info_form_msg").hide();
					var urlSendSms = $("#url_send_sms").val();
					$.post(urlSendSms,{mobile:mobile},function(ret){
						if(ret.success===true)
						{
							//var msg = "<span class='second'>60</span>秒后重发";
							smsBtnInterval();
							//lockList.mobile={msg:msg,time:new Date().getTime()};
						}
						else
						{
							if(ret.extra_params && ret.extra_params.live_time)
							{
								smsBtnInterval(ret.extra_params.live_time);
							}
							else
							{
								resetSmsBtn();
								me.attr("disabled",false).html("重发");
							}
							$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							/*if(!lockList.moblie)
							 {
							 lockList.mobile={msg:ret.error,time:new Date().getTime()};
							 }*/

						}
						//phoneDom.data("lockList",lockList);
					},'json');
					return false;
				});

				$("#valid_phone_form").find('.save_valid_phone').on('click', function(){
					var form = $("#valid_phone_form");
					if(form.valid())
					{
						var btn = $(this);
						btn.attr("disabled",true).text("操作中...");
						var urlSubmit =  $("#url_submit").val();
						$.post(urlSubmit,form.serializeArray(),function(ret){
							if(ret.success && ret.redirect)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("提交");
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});
				var newEmailFormValid = $("#new_email_form").validate({
					/*onfocusout : function(element) {
					 $(element).valid();
					 },*/
					showErrors:function(error,arr){
						this.defaultShowErrors(arguments);
						/*if(error.phone)
						{
							$(".send-sms").attr("disabled",true);
						}
						$.each(arr,function(k,v){
							//console.log(v.element);
							var el = $(v.element);
							el.parent().find('.success').removeClass('right_icon');
						});*/
					},
					rules: {
						email:{
							required:true,
							maxlength:60,
							check_email:true
						},
						code:{
							required:true,
							depend:"#mobile",
							number:true/*,
							rangelength:[6,6]*/
						}
					},
					messages: {
						email: {
							required:"邮箱不能为空",
							maxlength: $.validator.format('邮箱不允许超过{0}个字符'),
							check_email: '邮箱地址格式错误'
						},
						code:{
							required:"短信验证码不能为空",
							//number:$.validator.format("{0}位数字验证码"),
							maxlength:$.validator.format("{0}位数字验证码"),
							rangelength:$.validator.format("{0}位数字验证码"),
							depend:"请先填写正确的手机号码"
						}
					},
					errorPlacement:function(error,element){
						element.parent().find("label.checked").remove();
						//console.log(arguments);
						/*console.log("heihei");
						 if(element.attr('type') == 'password'){
						 if(element.val() == ''){
						 error.addClass('pwd_empty');
						 } else {
						 error.removeClass('pwd_empty');
						 }
						 }*/

						error.appendTo(element.parent());
					}
				});
				$("#new_email_form").find('.save_new_email').on('click', function(){
					var form = $("#new_email_form");
					if(form.valid()){
						var btn = $(this);
						btn.attr("disabled",true).text("操作中...");
						$.post("/user/send_new_email",form.serializeArray(),function(ret){
							if(ret.success && ret.redirect)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("提交");
								show_error(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});

				$("#mobile").keyup(function(e){
					if(/^1[0-9]{10}$/.test($(this).val()))
					{
						$(this).valid();
					}
				});
				function check_form_edit(form)
				{
					var edit = false;
					form.find("[name]").each(function(){
						/*if($(this).attr("type")=="hidden")
						{
							return;
						}*/
						//console.log($(this).val()+"....."+$(this)[0].defaultValue);
						if($(this).val()!=$(this)[0].defaultValue)
						{
							edit = true;
							return false;
						}
					});
					return edit;
				}
				function set_form_default_value(form)
				{
					form.find("[name]").each(function(){
						$(this)[0].defaultValue=$(this).val();
					});
				}
				var userInfoBtnSetimeout;
				$("#user_info_form").find('.blue_btn').on('click', function(){
					if(userInfoBtnSetimeout)
					{
						clearTimeout(userInfoBtnSetimeout);
					}
					var form =$("#user_info_form");
					if(form.valid())
					{
						var btn=$(this);
						var msgBox = $("#user_info_form_msg");
						if(!check_form_edit(form))
						{
							msgBox.show().removeClass("success").find("span").text("没有修改任何内容");
							return;
						}

						btn.attr("disabled",true).text("保存中...");

						msgBox.hide();
						$.post("/user/update_user_info",form.serializeArray(),function(ret){
							btn.attr("disabled",false).text("保存");
							if(ret.success)
							{
								set_form_default_value(form);
								msgBox.show().addClass("success").find("span").text("保存成功");
								userInfoBtnSetimeout = setTimeout(function(){
									msgBox.hide();
								},3000);
							/*<span class="display_field"></span>
							<a class="a_btn" href="javascript:void(0);">修改邮箱</a>*/
								var nEmail = $("form [name='n_email']");
								var nMobile = $("form [name='n_mobile']");
								if(nEmail.length>0 && $("#valid_email").length<=0)
								{
									var nEmailVal = nEmail.val();
									$("form [for='"+nEmail.attr("id")+"']").append('<span class="valid_text  no_valid">未验证</span>');
									nEmail.parent().append('<a  class="a_btn" id="valid_email" href="javascript:void(0);" data-email="'+nEmailVal+'">验证邮箱</a>');
								}
								if(nMobile.length>0 && $("#valid_mobile").length<=0)
								{
									var nMobileVal = nMobile.val();
									$("form [for='"+nMobile.attr("id")+"']").append('<span class="valid_text  no_valid">未验证</span>');
									nMobile.parent().append('<a class="a_btn" id="valid_mobile" href="/user/valid_phone">验证手机</a>');
								}
								$("#valid_email,#valid_phone").show();
							}
							else
							{
								msgBox.show().removeClass("success").find("span").text(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});
				//$("#user_info_form #email").blur();
				$("#user_info_form #email").keyup(function(){
					var me=$(this);
					if(me.val()=="")
					{
						$("#valid_email").hide();
					}
					else
					{
						$("#valid_email").show();
					}
				});
				$("#user_info_form #mobile").keyup(function(){
					var me=$(this);
					if(me.val()=="")
					{
						$("#valid_mobile").hide();
					}
					else
					{
						$("#valid_mobile").show();
					}
				});
				$("#user_info_form").delegate("#valid_email","click",function(){
					var me=$(this);
					if(!me.is(".loading"))
					{
						me.text("正在发送验证邮件...").addClass("loading");
						$("#user_info_form_msg").hide();
						$.post("/user/send_valid_email",{email:$("#email").val()},function(ret){
							if(ret.success)
							{
								location.href = ret.redirect;
							}
							else
							{
								me.text("验证邮箱").removeClass("loading");
								$("#user_info_form_msg").removeClass("success").show().find("span").text(ret.message);
							}
						},'json');
					}
				});
				
				/*昵称限制输入*/
				$('#nickname').on('keyup', function(){
					var val = $(this).val();
					var length = countCharacters(val);
					
					if(length >= 41){
						$(this).val(subString(val, 40));
					}
				});
				
				/*公司名称限制输入*/
				$('#company_name').on('keyup', function(){
					var val = $(this).val();
					var length = countCharacters(val);
					
					if(length >= 101){
						$(this).val(subString(val, 100));
					}
				});
				
				/*上传头像*/
				/*
				$('#js_upload').on('click', function(){
					$('#upload_file').click();
				});
				*/
				$('#upload_file').hover(
					function()
					{
						$("#js_default").fadeIn("fast");
						//$("#js_img").hide();
					},
					function(){
						//$("#js_img").show();
						if(!$('#js_upload').is(".loading"))
						{
							$("#js_default").fadeOut("fast");
						}

					}
				);
				$('#upload_file').on('click', function(){
					$(this).blur();
				});
				$('#upload_file').on('change', function(){
					var me=$(this);
					if(!me.val())
					{
						return;
					}

					//me.attr("disabled",true);
					me.hide();
					$('#js_upload').addClass("loading");
					$('#upload_error').html('').addClass("hide");

					$('#upload_form').ajaxSubmit({
						dataType: 'json',
						success: function(ret){
							me.show();
							$('#js_upload').removeClass("loading");
							if(ret.success){
								$('#js_upload .img-box img').attr("src",ret.img);
								$('#user_face').val(ret.img);
								$('#upload_error').html('').addClass("hide");
							} else {
								$('#upload_error').html('<i class="icon waring_tip"></i>'+ret.message).removeClass("hide");
							}
							$("#js_default").hide();
						}
					});
				});
				
				/*修改密码*/
				if($.validator){
					$.validator.addMethod("check_old_new", function(value, element) {
						return this.optional(element) || (value != $('#old_password').val());
					}, '新旧密码不能相同');
				};
				$("#update_pwd_form").validate({
					rules: {
						old_password:{
							required: true
						},
						new_password:{
							required: true,
							check_password: true,
							check_old_new: true,
							rangelength: [8, 16]
						},
						confirm_password:{
							required: true,
							equalTo: "#new_password",
							rangelength: [8, 16]
						}
						
					},
					messages: {
						old_password: {
							required: '旧密码不允许为空'
						},
						new_password: {
							required: '新密码不允许为空',
							rangelength: $.validator.format('由{0}-{1}位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字')
						},
						confirm_password: {
							required: '确认新密码不允许为空',
							equalTo: '两次输入的密码不一致',
							rangelength: $.validator.format('由{0}-{1}位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字')

						}
					},
					errorPlacement:function(error,element){
						element.parent().find('.success').removeClass('right_icon');
						element.parent().find("label.checked").remove();
						if(element.attr('name') == 'new_password'){
							if(element.val() == ''){
								error.addClass('pwd_empty');
							} else {
								error.removeClass('pwd_empty');
							}
							
						}
						error.appendTo(element.parent());
					},
					success: function(label) {
						label.html('').attr('class', 'checked');
						label.parent().find('.success').addClass('right_icon');
					}
				});
				$('#new_password').on({
					'keyup':function(){
						password_error_style($(this), $('#old_password'));
					},
					'blur':function(){
						password_error_style($(this), $('#old_password'));
					}
				});
				function password_error_style($this, $comp){
					if($this.val() == '' || $this.val() == $comp.val()){
						$('#new_password-error').addClass('pwd_empty');
					} else {
						$('#new_password-error').removeClass('pwd_empty');
					}
				}
				$("#update_pwd_form").find('.submit_btn').on('click', function(){
					var form = $("#update_pwd_form");
					if(form.valid()){
						var btn = $(this);
						btn.attr("disabled",true).text("操作中...");
						$.post(form.attr("action"),form.serializeArray(),function(ret){
							if(ret.success && ret.redirect)
							{
								location.href = ret.redirect;
							}
							else
							{
								btn.attr("disabled",false).text("提交");
								show_error(ret.message);
							}
						},'json');
						return true;
					}
					return false;
				});


				//注册面板的tab事件
				$(".reg-tab-nav li").click(function(){
					var me = $(this);
					//location.hash = me.attr("data-tab");
					if(!me.hasClass("active"))
					{
						regTabActive(me.attr("data-tab"));
						/*$(".reg-tab-nav li.active").removeClass("active");
						me.addClass("active");
						var tab = me.attr("data-tab");
						$(".reg-tab-content.active").removeClass("active");
						$(".reg-tab-content[data-tab='"+tab+"']").addClass("active");*/
					}
				});

				function regTabActive(tab)
				{
					location.hash = tab;
					var me = $(".reg-tab-nav li[data-tab='"+tab+"']");
					$(".reg-tab-nav li.active").removeClass("active");
					me.addClass("active");
					var tab = me.attr("data-tab");
					$(".reg-tab-content.active").removeClass("active");
					$(".reg-tab-content[data-tab='"+tab+"']").addClass("active").find("input[placeholder]").trigger("setcss");
				}
				if(location.pathname=="/register" || location.pathname=="/register/index")
				{
					if(location.hash=="#email")
					{
						regTabActive("email");
					}
					else
					{
						regTabActive("phone");
					}
				}

				
			});


			/*计算包含英文与汉字的字符串长度*/
			function countCharacters(str){
				var totalCount = 0; 
				for (var i=0; i<str.length; i++) { 
					var c = str.charCodeAt(i); 
					if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { 
						 totalCount++; 
					 }else {     
						 totalCount+=2; 
					 } 
				 }
				return totalCount;
			}

			/*截取中英文字符串*/
			function subString(str, len) {
				var newLength = 0;
				var newStr = "";
				var chineseRegex = /[^\x00-\xff]/g;
				var singleChar = "";
				var strLength = str.replace(chineseRegex,"**").length;
				for(var i = 0;i < strLength;i++) {
						singleChar = str.charAt(i).toString();
						if(singleChar.match(chineseRegex) != null) {
								newLength += 2;
						}else {
								newLength++;
						}
						if(newLength > len) {
								break;
						}
								newStr += singleChar;
				}
				return newStr;
			}
			
			
			
			function setErrorTime(module){
				module = arguments[0] ? arguments[0] : '';
				var time = setTimeout(function(){
					clearTimeout(time);
					var error_info = '<label id="j-error-time" class="error">请求超时，请刷新页面重新提交</label>';
					if(module == 'reg'){
						if($('#j-error-time').size() == 0){
							$('#reg_form').before(error_info);
						}
					} else {
						$('#form_error').html(error_info);
					}
				}, 1000*60);
				return time;
			}




		}
  };
});

